<template>
  <a-modal
    :title="$t('划拨')"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okText="$t('保存')"
    @cancel="handleCancel"
    :cancelText="$t('关闭')"
    v-drag
  >
    <a-form class="batchForm">
      <a-form-item :label="$t('绑定项目')" :labelCol="{ span: 7 }" :wrapperCol="{ span: 15 }">
        <a-select show-search :filter-option="false" @search="handleSearch" v-model="model.game">
          <a-select-option v-for="(item, key) in gameList" :key="key" :value="item.value">
              <span style="display: inline-block;width: 100%" :title="item.label ">
                {{ item.label }}
              </span>
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import {getAction, putAction} from '@/api/manage'

export default {
  name: 'BatchAssignModal',
  data() {
    return {
      width: 600,
      visible: false,
      model: {
        game: ""
      },
      gameList: [],
      deviceIds: [],
      queryList: []
    }
  },
  methods: {
    handleSearch(value) {
      let query = {}
      query.pageNo = 1
      query.pageSize = 99
      if(value){
        query.name=','+value+','
      }
      getAction('/game/bisGame/list', query).then((res) => {
        if (res.success) {
          this.gameList = [];
          res.result.records.forEach((r) => {
            this.gameList.push({
              value: r.id,
              label: r.name,
            });
          });
        }
      })
    },
    edit(datas) {
      this.deviceIds = datas;
      this.handleSearch('');
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      if (that.model.game == ""){
        that.$message.warning(that.$t('请选择绑定项目'))
        return;
      }
      that.deviceIds.forEach(item=>{
        let aa = {}
        aa = that.model
        aa = Object.assign({}, that.model)
        aa.id = item
        that.queryList.push(aa)
      })
      putAction('/device/bisDeviceInfo/updateBatch', that.queryList).then(res => {
        if (res.success) {
            that.$message.success(res.message)
            that.visible = false
            that.$emit('ok')
        } else {
            that.$message.warning(res.message)
            that.queryList = []
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style>
  .batchForm .ant-form-item-label{
    width: 20%;
  }
  .batchForm .ant-form-item-control-wrapper{
    width: 62.5%;
  }
</style>